package com.yaoweigroup.community.ui.home;

import java.util.ArrayList;
import java.util.List;

import com.yaoweigroup.community.R;
import com.yaoweigroup.community.adapter.AppsAdapter;
import com.yaoweigroup.community.db.DBUtil;
import com.yaoweigroup.community.enity.HomeIcon;
import com.yaoweigroup.community.enity.TypeIcon;
import com.yaoweigroup.community.ui.MainTabActivity;
import com.yaoweigroup.community.util.LogUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class AppsActivity extends Activity implements OnClickListener {

	private ListView listView;
	private RelativeLayout left_rl;
	private AppsAdapter adapter;
	private List<HomeIcon> list;
	private DBUtil dbUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_apps);

		listView = (ListView) findViewById(R.id.listView);
		left_rl = (RelativeLayout) findViewById(R.id.left_rl);
		left_rl.setOnClickListener(this);

		dbUtil = DBUtil.newInstance(this);
		initRes();
		adapter = new AppsAdapter(this, list);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {

				HomeIcon homes = list.get(position);
				try {
					if (homes.isSelect()) {
						dbUtil.db.delete(homes);
					} else {
						List<HomeIcon> selects = dbUtil.db
								.findAll(HomeIcon.class);
						if (selects.size() == 8) {
							Toast.makeText(AppsActivity.this, "亲 请删除其它的再添加",
									500).show();
							return;
						}
						dbUtil.db.saveOrUpdate(homes);
					}
					initRes();
				} catch (Exception e) {
					LogUtil.e("wxf", e.toString());
				}
			}
		});

	}

	private void initRes() {
		list = new ArrayList<HomeIcon>();
		HomeIcon home = new HomeIcon();
		home.setResId(R.drawable.home_wy_icon);
		home.setTypeName("在线物业");
		home.setClassName("com.yaoweigroup.community.ui.home.OnlineWyActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.home_llq_icon);
		home.setTypeName("邻里圈");
		home.setClassName("com.yaoweigroup.community.ui.home.BBSMainActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.home_group_icon);
		home.setTypeName("团购");
		home.setClassName("com.yaoweigroup.community.ui.home.GroupActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.go_hc_icon);
		home.setTypeName("公交换乘");
		home.setClassName("com.yaoweigroup.community.ui.go.ChangeLineActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.go_park_icon);
		home.setTypeName("停车位查询");
		home.setClassName("com.yaoweigroup.community.ui.go.ParkActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.go_dz_icon);
		home.setTypeName("出租电招");
		home.setClassName("com.yaoweigroup.community.ui.go.RendCarActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.go_zxc_icon);
		home.setTypeName("公共自行车");
		home.setClassName("com.yaoweigroup.community.ui.go.PubCarActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.home_gj_icon);
		home.setTypeName("实时公交");
		home.setClassName("com.yaoweigroup.community.ui.go.PubCarActivity");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.home_evcard_icon);
		home.setClassName("com.yaoweigroup.community.ui.go.EvcardActivity");
		home.setTypeName("EVCARD");
		list.add(home);

		home = new HomeIcon();
		home.setResId(R.drawable.discount_icon);
		home.setClassName("com.yaoweigroup.community.ui.home.DiscountActivity");
		home.setTypeName("优惠活动");
		list.add(home);

		try {
			List<HomeIcon> homes = dbUtil.db.findAll(HomeIcon.class);
			if (homes != null && homes.size() > 0) {
				for (HomeIcon h : list) {
					for (HomeIcon select : homes) {
						if (h.getTypeName().equals(select.getTypeName())) {
							h.setSelect(true);
							LogUtil.d("wxf", h.getTypeName() + ", true");
						}
					}
				}
			}
			adapter.setDataChange(list);
		} catch (Exception e) {
			LogUtil.e("wxf", e.toString());
		}

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.left_rl:
			finish();
			break;
		}
	}

}
